package common;

import dbhandler.DbCon;
import dbhandler.Guest;
import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class SearchGuest {

    private ArrayList<Integer> guestNo;
    private ArrayList<Guest> guest;
    private String fName;
    private String mName;
    private String lName;

    /**
     *
     * @param fName
     * @param mName
     * @param lName
     */
    public SearchGuest(String fName, String mName, String lName) {
        this.fName = fName;
        this.mName = mName;
        this.lName = lName;
        guestNo = new ArrayList();
        guest = new ArrayList();
    }

    /**
     *
     */
    public void search() {
        //the program searches for guests based on the first name of the guest
        if (!fName.isEmpty()) {
            try {
                ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM guest WHERE firstName ='" + fName + "'");
                int t = 0;
                while (rs.next()) {
                    guestNo.add(rs.getInt("guestNo"));
                    t = t + 1;
                }
                for (int i = 0; i < guestNo.size(); i++) {
                    guest.add(new Guest(guestNo.get(i)));
                }
            } catch (SQLException ex) {
                new EventMessage("SQL error in SearchGuest.search()", "There was an error performing the sql inquery\n").showException(ex);
            }
        } else {
            //the program searches for guests based on the middle name of the guest
            if (!mName.isEmpty() && guest.isEmpty()) {
                try {
                    ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM guest WHERE middleName ='" + mName + "'");
                    int t = 0;
                    while (rs.next()) {
                        guestNo.add(rs.getInt("guestNo"));
                        t = t + 1;
                    }

                    for (int i = 0; i < guestNo.size(); i++) {
                        guest.add(new Guest(guestNo.get(i)));
                    }
                } catch (SQLException ex) {
                    new EventMessage("SQL error in SearchGuest.search()", "There was an error performing the sql inquery\n").showException(ex);
                }
            } else {
                //the program searches for guests based on the last name of the guest
                if (!lName.isEmpty() && guest.isEmpty()) {
                    try {
                        ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM guest WHERE middleName ='" + lName + "'");
                        int t = 0;
                        while (rs.next()) {
                            guestNo.add(rs.getInt("guestNo"));
                            t = t + 1;
                        }

                        for (int i = 0; i < guestNo.size(); i++) {
                            guest.add(new Guest(guestNo.get(i)));
                        }
                    } catch (SQLException ex) {
                        new EventMessage("SQL error in SearchGuest.search()", "There was an error performing the sql inquery\n").showException(ex);
                    }
                }
            }
        }
    }

    /**
     *
     * @return
     */
    public ArrayList<Guest> getGuest() {
        return guest;
    }
}
